package com.lemon.api.auto.util;

import com.lemon.api.auto.pojo.Case;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ExcelUtil {
    public static List<Case> read(String filepath, String sheetName){
        //1.创建workbook对象
        InputStream inp = null;
        ArrayList<Case> list = new ArrayList<Case>();
        Object[][] datas=null;
        try {
            inp = new FileInputStream(new File((filepath)));
            Workbook workbook = WorkbookFactory.create(inp);
            //2.创建表单对象sheet
            Sheet sheet = workbook.getSheet(sheetName);
            workbook.getSheet(sheetName);
            int lastRowNum = sheet.getLastRowNum();
            //处理标题行
            Row titleRow = sheet.getRow(0);
            //获取最后的列号（此列号比索引大1）
            int lastCellNum = titleRow.getLastCellNum();
            //创建数组保存标题
            java.lang.String[] titles=new java.lang.String[lastCellNum];
            //通过循环依次取出标题的每一列
            for (int i = 0; i <= lastCellNum-1; i++) {
                Cell cell = titleRow.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                cell.setCellType(CellType.STRING);
                String cellValue = cell.getStringCellValue();
                titles[i]= cellValue;
            }
            //处理数据行（非标题行开始到左后一行）
            for (int i = 1; i <lastRowNum; i++) {
                //每次创建一个Case对象
                Case cs = new Case();
                //获取到i对应的数据行
                Row dataRow = sheet.getRow(i);
                //循环处理每一行上的所有列
                for (int j = 0; j <lastCellNum-1; j++) {
                    Cell cell = dataRow.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                    cell.setCellType(CellType.STRING);
                    String cellValue = cell.getStringCellValue();
                    String cellName = titles[j];
                    cellName = cellName.substring(0, cellName.indexOf("("));
                    String reflectMethodName="set"+cellName;
                    Method method = Case.class.getDeclaredMethod(reflectMethodName, String.class);
                    method.invoke(cs,cellValue);
                    list.add(cs);
                }
            }

            System.out.println(Arrays.toString(titles));
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (inp != null) {
                try {
                    inp.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    public static void main(String[] args) {
        String path="src/test/resources/cases_v3.xlsx";
        read(path,"用例");
        List<Case> cs = read("src/test/resources/cases_v3.xlsx", "用例");
        for (Case c : cs) {
            System.out.println(c);
        }
    }


}
